﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace CC.Yacht.Windows.Forms
{
    public partial class UserListBox : UserControl
    {
        #region Constructor
        public UserListBox()
        {
            InitializeComponent();

            /*
            for (int i = 0; i < 100; i++)
            {
                _ListViewUsers.Items.Add((i%2 == 0 ? "A" : "B") + i);
            }

            _ListViewUsers.Sort();
             */
        }
        #endregion

        #region Private Event Handlers
        private void _ContextMenuStripUserList_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (_ListViewUsers.SelectedItems.Count <= 0)
            {
                e.Cancel = true;
            }
        }

        private void _ListViewUsers_Resize(object sender, EventArgs e)
        {
            _ListViewUsers.Columns[0].Width = _ListViewUsers.ClientSize.Width - 4;
        }

        private void _SendMessageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //TODO: Implement private messaging...
        }
        #endregion

        #region Public Methods
        public void AddUser(User user, bool performSort)
        {
            if (!_ListViewUsers.Items.ContainsKey(user.UserId.ToString()))
            {
                _ListViewUsers.Items.Add(user.UserId.ToString(), user.Username, 0);
            }

            if (performSort)
            {
                _ListViewUsers.Sort();
            }
        }

        public void Clear()
        {
            _ListViewUsers.Items.Clear();
        }

        public void RemoveUser(User user)
        {
            if (_ListViewUsers.Items.ContainsKey(user.UserId.ToString()))
            {
                _ListViewUsers.Items.RemoveByKey(user.UserId.ToString());
            }
        }

        public void SetUsers(List<User> users)
        {
            _ListViewUsers.Items.Clear();

            for (int i = 0; i < users.Count; i++)
            {
                AddUser(users[i], false);
            }

            _ListViewUsers.Sort();
        }
        #endregion
    }
}
